import React from 'react';
import { Navigate } from 'react-router-dom';
import { useAuth } from '../../contexts/AuthContext';

const PublicRoute = ({ children }) => {
  const { isAuthenticated, loading } = useAuth();

  // 如果正在加载，显示加载状态
  if (loading) {
    return null;
  }

  // 如果已登录，重定向到首页
  if (isAuthenticated()) {
    return <Navigate to="/" replace />;
  }

  // 未登录，允许访问
  return children;
};

export default PublicRoute; 